package com.cysion.study.adapter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.cysion.study.ktbox.base.BaseAdapter
import com.cysion.study.ktbox.base.BaseViewHolder
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.uibox.custom.FlowLayout
import com.cysion.study.R
import com.cysion.study.entity.CourseEntity
import com.cysion.study.entity.Courses
import com.cysion.study.entity.Days
import com.cysion.study.ui.CallRollActivity
import kotlinx.android.synthetic.main.item_course_more.view.*

class CourseMoreAdapter(mEntities: MutableList<CourseEntity>, mContext: Context) :
    BaseAdapter<CourseEntity>(mEntities, mContext) {

    companion object {

        //点击事件
        const val refuse = 2000
        const val agree = 2001
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<CourseEntity> {
        return ProMoneyHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_course_more, parent, false)
        )
    }

    class ProMoneyHolder(itemView: View) : BaseViewHolder<CourseEntity>(itemView) {
        override fun fillData(obj: CourseEntity, position: Int) {
            itemView?.apply {
                //                itemView.tv_data.text=
                itemView.tv_weekday.text = obj.weekStart + "----" + obj.weekEnd
                val days = obj.days
                fl_view.removeAllViews()
                CLog.i("###每天" + days.size)
                if (days.size > 0) {
                    for (day in days) {
//                        initFlow(context, fl_view, si)//每天课程集合
                        val iv = LayoutInflater.from(context).inflate(R.layout.flow_course_layout, fl_view, false)
                        var tv_week = iv.findViewById<TextView>(R.id.tv_week)
                        var fl_source = iv.findViewById<FlowLayout>(R.id.fl_source)
                        tv_week.text = day.weeks + "(" + day.times + ")"
                        fl_source.removeAllViews()
                        if (day.courses.size > 0) {
                            for (item in day.courses) {
                                CLog.i("###每天课" + day.courses.size + item.name)
//                                initItem(context,fl_source,item)
                                val itemfl =
                                    LayoutInflater.from(context).inflate(R.layout.flow_course_item, fl_source, false)
                                var tv_source = itemfl.findViewById<TextView>(R.id.tv_source)
                                tv_source.text =
                                    item.startTime + "-" + item.endTime + "\n" + item.name + "(" + item.maxNum + "人)"
                                tv_source.setOnClickListener {
//                                    context.startActivity(Intent(context, CallRollActivity::class.java))//学生点名
                                    var intent = Intent(context, CallRollActivity::class.java)
                                    var bundle = Bundle()
                                    bundle.putString("caid", item.caid)
                                    intent.putExtras(bundle)
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                    context.startActivity(intent)
                                }
                                fl_source.addView(itemfl)
                            }
                        }
                        fl_view.addView(iv)
                    }
                }
            }
        }

        private fun initFlow(
            context: Context, flView: FlowLayout, days: Days
        ) {
            val iv = LayoutInflater.from(context).inflate(R.layout.flow_course_layout, flView, false)
            var tv_week = iv.findViewById<TextView>(R.id.tv_week)
            var fl_source = iv.findViewById<FlowLayout>(R.id.fl_source)
            tv_week.text = days.weeks + "(" + days.times + ")"
            fl_source.removeAllViews()
            if (days.courses.size > 0) {
                for (item in days.courses) {
                    initItem(context, fl_source, item)
                }
            }
            /*ivAdd.setOnClickListener {
                startActivityForResult_ex<FriendActivity>(401)
            }*/

            flView.addView(iv)
        }

        private fun initItem(context: Context, flSource: FlowLayout?, courses: Courses) {
            val iv = LayoutInflater.from(context).inflate(R.layout.flow_course_item, flSource, false)
            var tv_source = iv.findViewById<TextView>(R.id.tv_source)
            tv_source.text =
                courses.startTime + "-" + courses.endTime + "\n" + courses.name + "(" + courses.maxNum + "人)"
        }

    }
}